#!/bin/bash

# 确保X11权限

export DISPLAY=:0

xhost +local:root >/dev/null 2>&1

# 确保.Xauthority文件存在且可读
if [ ! -f "${XAUTHORITY:-$HOME/.Xauthority}" ]; then
    touch "$HOME/.Xauthority"
    xauth generate :0 . trusted 2>/dev/null || true
fi

show_help() {
    echo "用法: $0 [参数]"
    echo "  --os <name>         指定操作系统 (默认: openEuler)"
    echo "  -h, --help            显示帮助"
}

# 解析命令行参数
while [[ $# -gt 0 ]]; do
    case $1 in
        --os)   
            GO2_IMAGE_VERSION="$2"
            shift 2
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo "未知参数: $1"
            show_help
            exit 1
            ;;
    esac
done

# 设置默认值
export GO2_IMAGE_VERSION=${1:-openEuler}
export SRC_MOUNT_PATH=${2:-./src}
export UNITREE_GO_PATH=${3:-./unitree_go}
# export MEMORY_LIMIT=${4:-4G}
# export CPU_LIMIT=${5:-2.0}


# 如果是openEuler系统
if [ "$GO2_IMAGE_VERSION" == "openEuler" ]; then
    export DOCKER_IMAGE="agiros-docker:openEuler" 
    docker compose --env-file envs/openEuler.env up
# 如果是Ubuntu系统
elif [ "$GO2_IMAGE_VERSION" == "ubuntu" ]; then
    export DOCKER_IMAGE="agiros-docker:ubuntu"
    docker compose --env-file envs/ubuntu.env up
# 用户未配置，使用默认值
elif [ -z "$GO2_IMAGE_VERSION" ]; then
    export DOCKER_IMAGE="agiros-docker:openEuler"
    docker compose --env-file envs/openEuler.env up
# 不支持的操作系统
else
    echo "不支持的操作系统: $GO2_IMAGE_VERSION"
    show_help
    exit 1
fi
